In [1]:
from PIL import Image
import numpy as np
先下載 MNIST 資料
In [2]:
import os
import urllib
from urllib.request import urlretrieve
dataset = 'mnist.pkl.gz'
def reporthook(a,b,c):
print("\rdownloading: %5.1f%%"%(a*b*100.0/c), end="")
if not os.path.isfile(dataset):
origin = "https://github.com/mnielsen/neural-networks-and-deep-learning/raw/master/data/mnist.pkl.gz"
print('Downloading data from %s' % origin)
urlretrieve(origin, dataset, reporthook=reporthook)
In [3]:
import gzip
import pickle
with gzip.open(dataset, 'rb') as f:
train_set, validation_set, test_set = pickle.load(f, encoding='latin1')
In [4]:
%run -i q_see_mnist_data.py
In [5]:
train_X, train_y = train_set
test_X, test_y = test_set
In [6]:
# 訓練資料, y 的前 20 筆
train_y[:20]
Out[6]:
In [7]:
from IPython.display import display
def showX(X):
int_X = (X*255).clip(0,255).astype('uint8')
# N*784 -> N*28*28 -> 28*N*28 -> 28 * 28N
int_X_reshape = int_X.reshape(-1,28,28).swapaxes(0,1).reshape(28,-1)
display(Image.fromarray(int_X_reshape))
# 訓練資料, X 的前 20 筆
showX(train_X[:20])
In [8]:
%run -i q_square_error.py
In [9]:
%run -i q_find_nn_0.py
In [10]:
%run -i q_find_nn_10.py
In [11]:
# !可能會用掉太多記憶體!
#%run -i q_small_data.py
# accuracy: 85%
In [12]:
# 資料 normalize
train_X = train_X / np.linalg.norm(train_X, axis=1, keepdims=True)
test_X = test_X / np.linalg.norm(test_X, axis=1, keepdims=True)
In [13]:
# 矩陣乘法 == 大量計算內積
A = test_X @ train_X.T
print(A.shape)
In [14]:
A.argmax(axis=1)
Out[14]:
In [15]:
predict_y = train_y[A.argmax(axis=1)]
In [16]:
# 測試資料, X 的前 20 筆
showX(test_set[0][:20])
In [17]:
# 猜測的 Y 前20筆
predict_y[:20]
Out[17]:
In [18]:
#測試資料的 y 前 20 筆
test_y[:20]
Out[18]:
In [19]:
# 正確率
(predict_y == test_y).mean()
Out[19]:
In [20]:
from sklearn.decomposition import PCA
pca = PCA(n_components=60)
train_X = pca.fit_transform(train_set[0])
test_X = pca.transform(test_set[0])
In [21]:
train_X.shape
Out[21]:
In [22]:
train_X /= np.linalg.norm(train_X, axis=1, keepdims=True)
test_X /= np.linalg.norm(test_X, axis=1, keepdims=True)
In [23]:
# 矩陣乘法
A = test_X @ train_X.T
In [24]:
predict_y = train_y[A.argmax(axis=1)]
In [25]:
# 正確率
(predict_y == test_y).mean()
Out[25]: